/*
 * Copyright (C) 2017 Spreadtrum Communications Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "sprd_panel.h"
#include "sprd_dsi.h"
#include "dsi/mipi_dsi_api.h"
#include "sprd_dphy.h"

static uint8_t init_data[] = {
	0x29, 0x00, 0x00, 0x04, 0x60, 0x71, 0x23, 0xA2,
	0x29, 0x00, 0x00, 0x04, 0x60, 0x71, 0x23, 0xA3,
	0x29, 0x00, 0x00, 0x04, 0x60, 0x71, 0x23, 0xA4,
	0x29, 0x00, 0x00, 0x02, 0xCF, 0x18,
	0x29, 0x00, 0x00, 0x09, 0x85, 0x48, 0x8B, 0xEB, 0x11, 0x8B, 0x4A, 0x8F, 0xE6,
	0x29, 0x00, 0x00, 0x0D, 0x86, 0xA5, 0x19, 0x40, 0x15, 0xF5, 0x01, 0x00, 0xA5, 0xD9, 0x55, 0xF5, 0x0D,
	0x29, 0x00, 0x00, 0x08, 0xB0, 0x22, 0x70, 0x11, 0x7F, 0x25, 0x2F, 0x2F,
	0x29, 0x00, 0x00, 0x07, 0xD7, 0x00, 0x08, 0xDC, 0xAA, 0xD0, 0xD0,
	0x29, 0x00, 0x00, 0x02, 0x75, 0x2B,
	0x29, 0x00, 0x00, 0x0A, 0xB2, 0x15, 0x11, 0x05, 0x23, 0x41, 0xFF, 0x03, 0x52, 0x6B,
	0x29, 0x00, 0x00, 0x05, 0xB7, 0x00, 0x00, 0x5A, 0x5A,
	0x29, 0x00, 0x00, 0x06, 0xE8, 0x20, 0x6D, 0x06, 0x85, 0x85,
	0x29, 0x00, 0x00, 0x08, 0xBA, 0xB2, 0xB6, 0x52, 0xD6, 0x10, 0x24, 0x20,
	0x29, 0x00, 0x00, 0x02, 0xBF, 0x71,
	0x39, 0x00, 0x00, 0x28, 0xA3, 0x88, 0x01, 0x8C, 0x30, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x1E, 0x5C, 0x1E, 0x80, 0x00, 0x6F, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x1E, 0x5C, 0x1E, 0x80, 0x00, 0x20, 0x58, 0x00, 0x00, 0x00, 0xFF,
	0x39, 0x00, 0x00, 0x31, 0xA6, 0x5F, 0x18, 0x24, 0x84, 0x38, 0x00, 0x00, 0x01, 0x4D, 0x4D, 0x91, 0xFF, 0x08, 0x24, 0x84, 0x39, 0x00, 0x00, 0x00, 0x4D, 0x4D, 0x91, 0xFF, 0x48, 0xE4, 0x55, 0x07, 0x09, 0x07, 0x09, 0x4D, 0x4D, 0x91, 0xFF, 0x07, 0xDB, 0x55, 0x07, 0x09, 0x07, 0x09, 0x4D, 0x4D, 0xFF, 0xFF, 0x00, 0x08, 0x80,
	0x39, 0x00, 0x00, 0x3D, 0xA7, 0x1E, 0x19, 0x80, 0x64, 0x40, 0x07, 0x16, 0x49, 0x0A, 0x50, 0x0F, 0x4D, 0x4D, 0x91, 0xFF, 0x00, 0x80, 0x64, 0x40, 0x25, 0x34, 0x4B, 0x0C, 0x0E, 0x0D, 0x4D, 0x4D, 0x91, 0xFF, 0x00, 0x80, 0x64, 0x00, 0x4B, 0xC3, 0x46, 0x0E, 0x0D, 0x05, 0x4D, 0x4D, 0x91, 0xFF, 0x00, 0x80, 0x64, 0x00, 0x69, 0xE1, 0x08, 0x10, 0x2B, 0x03, 0x4D, 0x4D, 0x91, 0xFF, 0xF0, 0x80, 0x35,
	0x39, 0x00, 0x00, 0x11, 0xAD, 0xF0, 0x40, 0x06, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
	0x39, 0x00, 0x00, 0x2D, 0xAC, 0x18, 0x00, 0x03, 0x19, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x11, 0x13, 0x00, 0x00, 0x0A, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x01, 0x19, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x10, 0x12, 0x00, 0x00, 0x0B, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x29, 0x00, 0x00, 0x08, 0xAE, 0x1D, 0xE6, 0x00, 0x1D, 0xE6, 0x00, 0x00,
	0x39, 0x00, 0x00, 0x26, 0xC8, 0x00, 0x00, 0x1B, 0x31, 0x55, 0x00, 0x89, 0x03, 0xD0, 0x06, 0x11, 0x27, 0x07, 0x8C, 0x03, 0x21, 0xCD, 0x01, 0x02, 0x0F, 0x22, 0x35, 0x0E, 0x71, 0x09, 0x32, 0xD1, 0x0B, 0x28, 0x0B, 0xF3, 0x6F, 0x0C, 0xA4, 0xD1, 0x03, 0xED,
	0x39, 0x00, 0x00, 0x26, 0xC9, 0x00, 0x00, 0x1B, 0x31, 0x55, 0x00, 0x89, 0x03, 0xD0, 0x06, 0x11, 0x27, 0x07, 0x8C, 0x03, 0x21, 0xCD, 0x01, 0x02, 0x0F, 0x22, 0x35, 0x0E, 0x71, 0x09, 0x32, 0xD1, 0x0B, 0x28, 0x0B, 0xF3, 0x6F, 0x0C, 0xA4, 0xD1, 0x03, 0xED,
	0x39, 0x00, 0x00, 0x25, 0xE7, 0x3B, 0x00, 0x00, 0x7C, 0xA1, 0x8C, 0x20, 0x1A, 0xF0, 0xB1, 0x50, 0x00, 0x50, 0xB1, 0x50, 0xB1, 0x50, 0xD8, 0x00, 0x55, 0x00, 0xB1, 0x00, 0x45, 0xC9, 0x6A, 0xFF, 0x5A, 0xD8, 0x38, 0x88, 0x15, 0xB1, 0x01, 0x01, 0x71,
	0x29, 0x00, 0x00, 0x03, 0x89, 0x00, 0x00,
	0x05, 0x78, 0x00, 0x02, 0x11, 0x00,
	0x29, 0x64, 0x00, 0x08, 0xB0, 0x22, 0x70, 0x11, 0x7f, 0x25, 0x39, 0x39,
	0x05, 0x00, 0x00, 0x02, 0x29, 0x00,
	0x29, 0x00, 0x00, 0x02, 0x35, 0x00,
	CMD_END
};

static int mipi_dsi_send_cmds(struct sprd_dsi *dsi, void *data)
{
	uint16_t len;
	struct dsi_cmd_desc *cmds = data;

	if ((cmds == NULL) || (dsi == NULL)){
		pr_info("st7123 cmd or data is NULL!\n");
		return -1;
	}

	for (; cmds->data_type != CMD_END;) {
		len = (cmds->wc_h << 8) | cmds->wc_l;
		mipi_dsi_gen_write(dsi, cmds->payload, len);
		if (cmds->wait)
			msleep(cmds->wait);
		cmds = (struct dsi_cmd_desc *)(cmds->payload + len);
	}

	return 0;
}

static int st7123_init(void)
{
	struct sprd_dsi *dsi = &dsi_device;
	struct sprd_dphy *dphy = &dphy_device;

	mipi_dsi_lp_cmd_enable(dsi, true);
	mipi_dsi_send_cmds(dsi, init_data);
	msleep(5); // add by jetxu 20230526
	mipi_dsi_set_work_mode(dsi, SPRD_MIPI_MODE_VIDEO);
	mipi_dsi_state_reset(dsi);
	mipi_dphy_hs_clk_en(dphy, true);

	return 0;
}

static int st7123_readid(void)
{
	struct sprd_dsi *dsi = &dsi_device;
	uint8_t cmd[] = {0xFF, 0x98, 0x81, 0x01};
	uint8_t read_buf[4] = {0};

	return 0;

	mipi_dsi_lp_cmd_enable(dsi, true);
	mipi_dsi_gen_write(dsi, cmd, ARRAY_SIZE(cmd));
	mipi_dsi_set_max_return_size(dsi, 1);
	mipi_dsi_dcs_read(dsi, 0x00, &read_buf[0], 1);
	mipi_dsi_dcs_read(dsi, 0x01, &read_buf[1], 1);


	if ((read_buf[0] == 0x98) && (read_buf[1] == 0x81)) {
		pr_info("st7123 read id success!\n");
		return 0;
	}

	pr_err("st7123 read id failed!\n");
	return -1;
}

static int st7123_power(int on)
{
	if (on) {
		sprd_gpio_request(NULL, CONFIG_LCM_GPIO_AVDDEN);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_AVDDEN, 1);
		mdelay(10);

		sprd_gpio_request(NULL, CONFIG_LCM_GPIO_AVEEEN);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_AVEEEN, 1);
		mdelay(10);

		sprd_gpio_request(NULL, CONFIG_LCM_GPIO_RSTN);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 1);
		mdelay(100);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 0);
		mdelay(100);
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 1);
		mdelay(100);
	} else {
		sprd_gpio_direction_output(NULL, CONFIG_LCM_GPIO_RSTN, 0);
		mdelay(5);
	}

	return 0;
}

static struct panel_ops st7123_ops = {
	.init = st7123_init,
	.read_id = st7123_readid,
	.power = st7123_power,
};

static struct panel_info st7123_info = {
	/* common parameters */
	.lcd_name = "lcd_st7123_truly_mipi_hd",
	.type = SPRD_PANEL_TYPE_MIPI,
	.bpp = 24,
//	.fps = 3,
	.width = 720,
	.height = 1440,

	/* DPI specific parameters */
	.pixel_clk = 76800000,
	.rgb_timing = {
		.hfp = 32,
		.hbp = 42,
		.hsync = 4,
		.vfp = 220,
		.vbp = 6,
		.vsync = 2,
	},

	/* MIPI DSI specific parameters */
	.phy_freq = 552000,
	.lane_num = 4,
	.work_mode = SPRD_MIPI_MODE_VIDEO,
	.burst_mode = PANEL_VIDEO_BURST_MODE,
	.nc_clk_en = false,
};

struct panel_driver st7123_truly_driver = {
	.info = &st7123_info,
	.ops = &st7123_ops,
};
